//页面功能：从数据库获取日记数据并展示
package com.xxz.terminal;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

public class DiaryActivity extends AppCompatActivity {

    String diarydata;
    String date,status;
    TextView read,ym,day,wt;
    DBHelper dbHelper;
    SQLiteDatabase db;
    String user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary);

        Intent intent = getIntent();
        diarydata = intent.getStringExtra("diarydata");
        date = intent.getStringExtra("date");
        user_id = intent.getStringExtra("user_id");

        String d[] = date.split("/");

        String yms = d[0]+"年，"+d[1]+"月";
        String days = d[2];
        String wts = "星期"+d[6]+" "+d[3]+":"+d[4];

        read = findViewById(R.id.read);
        ym = findViewById(R.id.ym);
        day = findViewById(R.id.day);
        wt = findViewById(R.id.wt);

        read.setText(diarydata);
        read.setMovementMethod(ScrollingMovementMethod.getInstance());//使得文本框中文字过多时可滑动

        ym.setText(yms);
        day.setText(days);
        wt.setText(wts);

        dbHelper = new DBHelper(this);
        db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("diary_data",new String[]{"treehole_status"},"user_id=? and date=?",new String[]{user_id,date},
                null,null,null);
        while (cursor.moveToNext()){
            status = cursor.getString(cursor.getColumnIndexOrThrow("treehole_status"));}//找投递状态

    }

    public void change(View btn){
        //打开日记修改页面并传值
        Intent intent = new Intent(this,DiaryWriteActivity.class);
        intent.putExtra("diarydata",diarydata);
        startActivity(intent);
    }

    //删除当前日记
    public void delete(View btn){
        Intent intent = new Intent(this,MainPageActivity.class);

            if(status.equals("已投递")){
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示").setMessage("该随记已投递到树洞，请到我的投递中撤销").setPositiveButton("确定",null);
                builder.create().show();
            }
            else {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示").setMessage("请确认是否删除当前数据").setPositiveButton("是",new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        db.delete("diary_data","user_id=? and date=?",new String[]{user_id,date});//数据库记录删除
                        Toast.makeText(getApplication(), "删除成功", Toast.LENGTH_SHORT).show();
                        intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                        Toast.makeText(DiaryActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("否",null);
                builder.create().show();
            }
        }

    public void share(View btn){
        if(status.equals("已投递")){
            Toast.makeText(this, "已经投递过了哦", Toast.LENGTH_SHORT).show();
        }
        else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示").setMessage("要将这条随记上传到树洞和大家分享吗").setPositiveButton("是",new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dbHelper = new DBHelper(getApplicationContext());
                    db = dbHelper.getWritableDatabase();
                    LocalDateTime localDateTime = LocalDateTime.now();
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//用format标准化时间形式
                    String datetime = localDateTime.format(dtf);
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("treehole_status","已投递");
                    contentValues.put("treehole_time",datetime);
                    db.update("diary_data",contentValues,"user_id=? and date=?",new String[]{user_id,date});//数据库记录更新
                    Toast.makeText(getApplication(), "上传成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(DiaryActivity.this,MainPageActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }

            }).setNegativeButton("否",null);
            builder.create().show();
        }
    }
}